import 'package:flutter/material.dart';
import 'package:drm/util/loading_dialog.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:drm/loading/bloc.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:bloc/bloc.dart';


// 可用于页面状态的保持；而不重新刷新页面； 一般用于，主页面有page的或者listView比较长的。
//with AutomaticKeepAliveClientMixin
///
///  @override
//  // TODO: implement wantKeepAlive
//  bool get wantKeepAlive => true;
///
///
abstract class PageState<Page extends StatefulWidget> extends State<Page>
     {
  //是否显示网络进度栏
  Widget _progressBar = WidgetCreator.createCommonProgressBar();
  bool _showProgress = true;
  LoadingBloc _loadingBloc = LoadingBloc();
  //用于页面销毁时，统一销毁bloc；
  List<Bloc> _blocs =[] ;

   addBlocToBlocList();
   List<Bloc> getBlocs()=> this._blocs;

  @override
  Widget build(BuildContext context) {
    return Material(
      child: BlocBuilder(
          bloc: _loadingBloc,
          builder: (context, boolValue) {
            _showProgress = boolValue;
            return Stack(children: <Widget>[
              Material(
                child:  mainView(context),
              ),
              Offstage(
                child: new Center(
                  child: _progressBar,
                ),
                offstage: !_showProgress,
              ),
            ]);
          }),
    );
  }

  showToast(String msg) {
    Fluttertoast.showToast(
        msg: msg,
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.BOTTOM,
        timeInSecForIos: 1,
        backgroundColor: Colors.black45,
        textColor: Colors.white,
        fontSize: 16.0);
  }

  /*
   * 显示网络进度栏及关闭软键盘<br/>
   */
  void showLoading({String text}) {
    _loadingBloc.dispatch(LoadingEvent.SHOW);
  }

  /*
   * 隐藏并重置网络进度栏<br/>
   */
  void hideLoading() {
    _loadingBloc.dispatch(LoadingEvent.HIDE);
  }


  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _loadingBloc.dispose();

    _blocs.forEach((bloc) => bloc.dispose());
  }
  void close(BuildContext context) {
    Navigator.pop(context);
  }

  Widget mainView(BuildContext context);
}
